package com.telecomyt.jwportal.activity.textview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.base.BaseActivity;
import com.telecomyt.jwportal.base.BasePresenter;
import com.telecomyt.jwportal.utils.DisplayUtil;
import com.telecomyt.jwportal.utils.ToastUtils;
import com.telecomyt.jwportal.widget.listener.OnNoFastClickListener;
import com.telecomyt.jwportal.widget.textview.ExpandableTextView;
import com.telecomyt.jwportal.widget.textview.RunTextView;
import com.telecomyt.jwportal.widget.textview.verticalview.SimpleViewHolder;
import com.telecomyt.jwportal.widget.textview.verticalview.TextViewVertical;
import com.telecomyt.jwportal.widget.textview.verticalview.VerticalView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @date: 2020/10/21
 * @author: chengDa
 * @description: TextView
 */
public class TextViewActivity extends BaseActivity {


    @BindView(R.id.home_item_runtext)
    RunTextView homeItemRuntext;
    @BindView(R.id.home_item_vertical)
    TextViewVertical homeItemVertical;
    @BindView(R.id.home_item_verticalmore)
    TextViewVertical homeItemVerticalmore;
    @BindView(R.id.mExpandableTextView)
    ExpandableTextView mExpandableTextView;
    @BindView(R.id.mTextView)
    TextView mTextView;

    public static void start(Context context) {
        Intent starter = new Intent(context, TextViewActivity.class);
        context.startActivity(starter);
    }

    //    @BindView(R.id.magic_indicator)
//    MagicIndicator magicIndicator;
//    @BindView(R.id.view_pager)
//    ViewPager viewPager;
//    List<String> mTitleDataList = new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_text_view;
    }

    @Override
    protected void initView() {
        String runstr = "邻家小萝莉干了什么坏事，妈妈生气不理她了。爸爸温柔地告诉萝莉：妈妈在等你认错呢，快去吧。" +
                "萝莉就泪汪汪地过去对妈妈说：你……你是不是范冰冰？“啥？！”妈妈莫名其妙。萝莉继续泪汪汪：我认错了…";
        homeItemRuntext.setText(runstr);

        ArrayList<String> titleList = new ArrayList<>();
        //模拟数据1
        titleList.add("你是天上最受宠的一架钢琴");
        titleList.add("我是丑人脸上的鼻涕");
        titleList.add("你发出完美的声音");
        titleList.add("我被默默揩去");
        titleList.add("你冷酷外表下藏着诗情画意");
        titleList.add("我已经够胖还吃东西");
        titleList.add("你踏着七彩祥云离去");
        titleList.add("我被留在这里");

        //初始化TextViewVertical配置
        homeItemVerticalmore.setData(titleList, new TextViewVertical.Adapter<String>() {
            @Override
            public void fillVerticalItem(VerticalView itemView, @Nullable String model, int position) {
                itemView.setContentText(model);
            }

            @Override
            public void onVerticalItemClick(VerticalView itemView, @Nullable String model, int position) {
                ToastUtils.showShort("点击了 : " + position + "\nmodel" + model);
            }
        });

        homeItemVertical.setData(R.layout.layout_text_vertical, titleList, new TextViewVertical.ViewHolder<String>() {
            @Override
            public void onHolder(SimpleViewHolder holder, @Nullable final String model, final int position) {
                holder.setGone(R.id.empty_view_img, false);
                holder.setText(R.id.empty_view_tv, model);
                holder.setOnClickListener(R.id.empty_view_tv, new OnNoFastClickListener() {
                    @Override
                    protected void onNoDoubleClick(View view) {
                        ToastUtils.showShort("点击了 : " + position + "\nmodel" + model);
                    }
                });
            }
        });
        int viewWidth = getWindowManager().getDefaultDisplay().getWidth() - dp2px(this, 30f);
        mExpandableTextView.initWidth(viewWidth);
        mExpandableTextView.setMaxLines(2);
        mExpandableTextView.setHasAnimation(false);
        mExpandableTextView.setCloseInNewLine(true);
        mExpandableTextView.setOpenSuffixColor(getResources().getColor(R.color.colorAccent));
        mExpandableTextView.setCloseSuffixColor(getResources().getColor(R.color.colorAccent));
        mExpandableTextView.setOriginalText("邻家小萝莉干了什么坏事，妈妈生气不理她了。爸爸温柔地告诉萝莉：妈妈在等你认错呢，快去吧。萝莉就泪汪汪地过去对妈妈说：你……你是不是范冰冰？“啥？！”妈妈莫名其妙。萝莉继续泪汪汪：我认错了…"
        );

        addTagToTextView(mTextView, "邻家小萝莉干了什么坏事，妈妈生气不理她了。爸爸温柔地告诉萝莉：妈妈在等你认错呢", "文本");

    }

    private void addTagToTextView(TextView target, String title, String tag) {
        if (TextUtils.isEmpty(title)) {
            title = "";
        }

        String content = title + tag;


        /**
         * 创建TextView对象，设置drawable背景，设置字体样式，设置间距，设置文本等
         * 这里我们为了给TextView设置margin，给其添加了一个父容器LinearLayout。不过他俩都只是new出来的，不会添加进任何布局
         */
        LinearLayout layout = new LinearLayout(this);
        TextView textView = new TextView(this);
        textView.setText(tag);
//        textView.setBackground(getResources().getDrawable(R.mipmap.ic_record));
        textView.setTextSize(12);
        textView.setTextColor(Color.parseColor("#FDA413"));
        textView.setIncludeFontPadding(false);
        textView.setPadding(DisplayUtil.dip2px(this, 6), 0,
                DisplayUtil.dip2px(this, 6), 0);
        textView.setHeight(DisplayUtil.dip2px(this, 17));
        textView.setGravity(Gravity.CENTER_VERTICAL);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置左间距
        layoutParams.leftMargin = DisplayUtil.dip2px(this, 6);
        // 设置下间距，简单解决ImageSpan和文本竖直方向对齐的问题
        layoutParams.bottomMargin = DisplayUtil.dip2px(this, 3);
        layout.addView(textView, layoutParams);

        /**
         * 第二步，测量，绘制layout，生成对应的bitmap对象
         */
        layout.setDrawingCacheEnabled(true);
        layout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        // 给上方设置的margin留出空间
        layout.layout(0, 0, textView.getMeasuredWidth() + DisplayUtil.dip2px(this, (6 + 3)), textView.getMeasuredHeight());
        // 获取bitmap对象
        Bitmap bitmap = Bitmap.createBitmap(layout.getDrawingCache());
        //千万别忘最后一步
        layout.destroyDrawingCache();

        /**
         * 第三步，通过bitmap生成我们需要的ImageSpan对象
         */
        ImageSpan imageSpan = new ImageSpan(this, bitmap);


        /**
         * 第四步将ImageSpan对象设置到SpannableStringBuilder的对应位置
         */
        SpannableStringBuilder ssb = new SpannableStringBuilder(content);
        //将尾部tag字符用ImageSpan替换
        ssb.setSpan(imageSpan, title.length(), content.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        target.setText(ssb);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dp2px(Context context, float dpValue) {
        int res = 0;
        final float scale = context.getResources().getDisplayMetrics().density;
        if (dpValue < 0)
            res = -(int) (-dpValue * scale + 0.5f);
        else
            res = (int) (dpValue * scale + 0.5f);
        return res;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected BasePresenter creatPresenter() {
        return null;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}